***************************************************************
* Title: RoeWang2023_Code4Figures
* Author: Charles CY Wang
* Created: Feb 19, 2023
* Purpose: This code generates the main figures in Roe & Wang (2023): ""
***************************************************************

****Clears****
cls
clear all

****Choose Directory****
local dirpath "${dir}/N Public Firms/Data"

cd "`dirpath'"

**** Start Options****
set more off
global compdata "220527"
global mnadata "20221013"

//FIGURE 1: NUMBER OF PUBLIC FIRMS, 1990-2021
	use "B_Clean_Data/Compustat/regdata_final_${compdata}.dta", clear
	twoway line nfirms fyear if fyear>=1990, yaxis(1) lwidth(vthick) lpat(solid) lcolor(dkgreen) ///
		|| lfit nfirms fyear if fyear>=1990, yaxis(1) lwidth(normal) lpat(dash) lcolor(dkgreen) ///
		xlab(1990 (5) 2020,labsize(large)) scale(0.75) aspectratio(0.54) xsize(5) ysize(3) ///
		legend(off)	graphregion(fcolor(white) lcolor(white)) plotregion(style(none)) ///
		xtitle("") ytitle(" ") yscale(range(2500 7000) axis(1)) ylabel(3000[1000]7000, labsize(large)) 

			
//FIGURE 2: ANNUAL NUMBER OF IPOs, 1990-2021		
	use IPOData_Final, clear
	
	twoway line IPO_Ritter year if inrange(year,1990,2021)==1, yaxis(1) lwidth(vthick) lpat(solid) lcolor(dkgreen) ///
		|| lfit IPO_Ritter year if inrange(year,1990,2021)==1, yaxis(1) lwidth(normal) lpat(dash) lcolor(dkgreen) ///
		graphregion(fcolor(white) lcolor(white)) plotregion(style(none)) legend(off) ///
		xlab(1990 (5) 2020, labsize(large)) scale(0.75) aspectratio(0.54) xsize(5) ysize(3) ///
		xtitle("") ytitle("") yscale(range(0 700) axis(1)) ylabel(0[100]700, labsize(large)) 
			
	
//FIGURE 3: Total Stock Market Capitalization / GDP (1990-2021)	
	use "B_Clean_Data/Compustat/regdata_final_${compdata}.dta", clear
	
	twoway line mcap_gdp fyear if fyear>=1990, yaxis(1) lwidth(vthick) lpat(solid) lcolor(dkgreen) ///
		|| lfit mcap_gdp fyear if fyear>=1990, yaxis(1) lwidth(normal) lpat(dash) lcolor(dkgreen) ///
		legend(off)  graphregion(fcolor(white) lcolor(white)) plotregion(style(none)) ///
		xlab(1990 (5) 2020, labsize(large)) scale(0.75) aspectratio(0.54) xsize(5) ysize(3) ///
		xtitle("") ytitle("") yscale(range(0 2.5)) ylabel(0[0.5]2.5, labsize(large)) 
		
//FIGURE 4: Net Income / GDP (1990-2021)	
	use "B_Clean_Data/Compustat/regdata_final_${compdata}.dta", clear
	twoway line ni_gdp fyear if fyear>=1990, yaxis(1) lwidth(vthick) lpat(solid) lcolor(dkgreen) ///
		|| lfit ni_gdp fyear if fyear>=1990, yaxis(1) lwidth(normal) lpat(dash) lcolor(dkgreen) ///
		legend(off)  graphregion(fcolor(white) lcolor(white)) plotregion(style(none)) ///
		xlab(1990 (5) 2020, labsize(large)) scale(0.75) aspectratio(0.54) xsize(5) ysize(3) ///
		xtitle("") ytitle("") yscale(range(0 0.1)) ylabel(0[0.025]0.1, labsize(large)) 
	
//FIGURE 5: Investments / GDP (1990-2021)	
	use "B_Clean_Data/Compustat/regdata_final_${compdata}.dta", clear
	twoway line inv1_gdp fyear if fyear>=1990, yaxis(1) lwidth(vthick) lpat(solid) lcolor(dkgreen) ///
		|| lfit inv1_gdp fyear if fyear>=1990, yaxis(1) lwidth(normal) lpat(dash) lcolor(dkgreen) ///
		legend(off)  graphregion(fcolor(white) lcolor(white)) plotregion(style(none)) ///
		xlab(1990 (5) 2020, labsize(large)) scale(0.75) aspectratio(0.54) xsize(5) ysize(3) ///
		xtitle("") ytitle("") yscale(range(0.05 0.08)) ylabel(0.05[0.01]0.08, labsize(large)) 
		
//FIGURE 6: Revenues / GDP (1990-2021)	
	use "B_Clean_Data/Compustat/regdata_final_${compdata}.dta", clear
	twoway line revt_gdp fyear if fyear>=1990, yaxis(1) lwidth(vthick) lpat(solid) lcolor(dkgreen) ///
		|| lfit revt_gdp fyear if fyear>=1990, yaxis(1) lwidth(normal) lpat(dash) lcolor(dkgreen) ///
		legend(off)  graphregion(fcolor(white) lcolor(white)) plotregion(style(none)) ///
		xlab(1990 (5) 2020, labsize(large)) scale(0.75) aspectratio(0.54) xsize(5) ysize(3) ///
		xtitle("") ytitle("") yscale(range(0.65 0.9)) ylabel(0.65[0.05]0.9, labsize(large)) 
	
//FIGURE 7: Employment (1990-2021)		
	use "B_Clean_Data/Compustat/regdata_final_${compdata}.dta", clear		
	*rescale to millions (original compustat number in thousands)
	gen emp_millions= emp / 1000
	reg emp_millions fyear, robust
	gen emp_pop = (emp*1000/100000) / Pop100K
	reg emp_pop fyear, robust
	gen emp_blsemp = emp_millions / emp_bls
	twoway line emp_blsemp fyear if fyear>=1990, yaxis(1) lwidth(vthick) lpat(solid) lcolor(dkgreen) ///
		|| lfit emp_blsemp fyear if fyear>=1990, lwidth(normal) lpat(dash) lcolor(dkgreen) ///
		legend(off)  graphregion(fcolor(white) lcolor(white)) plotregion(style(none)) ///
		xlab(1990 (5) 2020, labsize(large)) scale(0.75) aspectratio(0.54) xsize(5) ysize(3) ///
		xtitle("") ytitle("") yscale(range(0.24 0.3)) ylabel(0.25[0.01]0.3, labsize(large)) 

//FIGURE 8: Distribution of Large Firm Acquisitions (1997-2021)		
	use mna_bysize_${mnadata}, clear
	keep if year >=1997
	collapse (sum) N TargetMCAP2021 AcquirorMCAP2021, by(t_quint a_quint)
	egen N_all = total(N)
	egen TMCAP_all = total(TargetMCAP2021)
	egen AMCAP_all = total(AcquirorMCAP2021)
	gen perc_N = N/N_all
	gen perc_TMCAP = TargetMCAP2021/TMCAP_all
	gen perc_AMCAP = AcquirorMCAP2021/AMCAP_all
	gen A_Cat = "Large" if a_quint>3
	replace A_Cat = "Small" if a_quint<3
	replace A_Cat = "Medium" if a_quint==3
	gen T_Cat = "Large" if t_quint>3
	replace T_Cat = "Small" if t_quint<3
	replace T_Cat = "Medium" if t_quint==3
	gen Cat=A_Cat+"-"+T_Cat
	collapse (sum) perc_N perc_TMCAP perc_AMCAP N TargetMCAP2021, by(Cat)
	
	keep if inlist(Cat,"Large-Large","Large-Medium","Large-Small")
	graph bar perc_TMCAP perc_N, over(Cat) blabel(total, format(%9.2f)) ///
	legend(region(lcolor(white)) size(small) cols(2) order(1 2) ///
	lab(2 "% of Total Transactions (4,041)") lab(1 "% of Total Target Mcap ($7.7B)")) ///
	graphregion(fcolor(white) lcolor(white)) plotregion(style(none)) ///
	scale(.75) aspectratio(.85) xsize(3.5) ysize(3) 


//FIGURE 9: Graph showing IPO numbers and implied mcap using Ritter data (1990-2021)			
	use IPOData_Final, clear

	twoway line IPO_Ritter year if inrange(year,1990,2021)==1, yaxis(1) lwidth(vthick) lpat(solid) lcolor(dkgreen) ///
		|| lfit IPO_Ritter year if inrange(year,1990,2021)==1, yaxis(1) lwidth(normal) lpat(dash) lcolor(dkgreen) ///
		graphregion(fcolor(white) lcolor(white)) plotregion(style(none)) legend(off) ///
		xlab(1990 (5) 2020, labsize(large)) scale(0.75) aspectratio(0.8) xsize(4) ysize(3.5) ///
		title("Number of IPOs") xtitle("") ytitle("") ///
		yscale(range(0 700) axis(1)) ylabel(0[100]700, labsize(large)) saving(IPOs.gph, replace)
	
	
	cap: gen IPOMcap_Ritter_gdp = (impliedmcap*1000+Proceeds_deSPAC)/gdp_ann
	twoway line IPOMcap_Ritter_gdp year if inrange(year,1990,2021)==1, yaxis(1) lwidth(vthick) lpat(solid) lcolor(dkgreen) ///
		|| lfit IPOMcap_Ritter_gdp year if inrange(year,1990,2021)==1, yaxis(1) lwidth(normal) lpat(dash) lcolor(dkgreen) ///
		graphregion(fcolor(white) lcolor(white)) plotregion(style(none)) legend(off) ///
		xlab(1990 (5) 2020, labsize(large)) scale(0.75) aspectratio(0.8) xsize(4) ysize(3.5) ///
		title("Aggregate Implied IPO Market Cap / GDP") xtitle("") ytitle("") ///
		yscale(range(0 0.07) axis(1)) ylabel(0[0.01]0.07, labsize(large)) saving(IPOMcap.gph, replace	)
	
	graph combine IPOs.gph IPOMcap.gph, col(2) ///
		graphregion(fcolor(white) lcolor(white)) plotregion(style(none)) ///
		scale(1) xsize(7.2) ysize(3)
	
	
	